﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Budget.Silver.OutcomeServiceReference;

namespace Budget.Silver.Model.Calendar
{
    public class MonthModel
    {
        private readonly int year, month;
        private readonly ICollection<DayModel> days;

        public ICollection<DayModel> Days
        {
            get
            {
                var newDays = new List<DayModel>();
                var firstDayWeek = (int) new DateTime(year, month, 1).DayOfWeek;
                for (int i = 0; i < firstDayWeek - 1; i++)
                {
                    newDays.Add(new DayModel());
                }
                newDays.AddRange(days);
                return newDays;
            }
        }

        public MonthModel(ICollection<OutcomeOrderData> orders, int year, int month)
        {
            this.year = year;
            this.month = month;
            var daysCount = DateTime.DaysInMonth(year, month);
            days = new Collection<DayModel>();
            for (int i = 0; i < daysCount; i++)
            {
                var date = new DateTime(year, month, i + 1);
                var dateAmount = orders.Where(o => o.CreationDate.Date == date.Date).Sum(o => o.Amount);
                days.Add(new DayModel(date, dateAmount));
            }
        }
    }
}